package com.yuqing.sentimentapp.workspace.heavyrainfallflood;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.workspace.heavyrainfallflood.entity.HeavyRainfallFloodInfoResult;
import com.yuqing.sentimentapp.workspace.heavyrainfallflood.entity.HeavyRainfallFloodReportResult;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 防疫物资
 * Created by shenhe on 2020/4/3.
 */
public class HeavyRainfallFloodFillInActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tv_save)
    TextView mTvSave;
    @BindView(R.id.et_actionC1)
    EditText mEtActionC1;
    @BindView(R.id.et_actionC2)
    EditText mEtActionC2;
    @BindView(R.id.et_actionC3)
    EditText mEtActionC3;
    @BindView(R.id.et_actionC4)
    EditText mEtActionC4;
    @BindView(R.id.et_actionC5)
    EditText mEtActionC5;
    @BindView(R.id.et_actionC6)
    EditText mEtActionC6;
    @BindView(R.id.et_staPersonCount)
    EditText mEtStaPersonCount;
    @BindView(R.id.et_staCarCount)
    EditText mEtStaCarCount;
    @BindView(R.id.et_staSafeCount)
    EditText mEtStaSafeCount;
    @BindView(R.id.et_staEvacuateCount)
    EditText mEtStaEvacuateCount;
    @BindView(R.id.et_staRescueProperty)
    EditText mEtStaRescueProperty;
    @BindView(R.id.et_staSafeProperty)
    EditText mEtStaSafeProperty;
    @BindView(R.id.et_staBoatCount)
    EditText etStaBoatCount;


    private boolean editEnable = true;
    private HeavyRainfallFloodInfoResult.DataBean actionInfo;
    private HeavyRainfallFloodReportResult.DataBean.SubSumBean subSum;
    /**
     * 从已上报列表跳转过来的
     */
    private HeavyRainfallFloodReportResult.DataBean.ReportsBean reportsInfo;

    public static void goActivity(Activity activity, HeavyRainfallFloodInfoResult.DataBean actionInfo, HeavyRainfallFloodReportResult.DataBean.SubSumBean subSum) {
        Intent intent = new Intent(activity, HeavyRainfallFloodFillInActivity.class);
        intent.putExtra("actionInfo", actionInfo);
        intent.putExtra("subSum", subSum);
        activity.startActivity(intent);
    }

    public static void goActivity(Activity activity, HeavyRainfallFloodReportResult.DataBean.ReportsBean reportsInfo) {
        Intent intent = new Intent(activity, HeavyRainfallFloodFillInActivity.class);
        intent.putExtra("reportsInfo", reportsInfo);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_heavy_rainfall_flood_fill_in;
    }

    @Override
    public void initPresenter() {
        Intent intent = getIntent();
        if (intent == null) return;
        actionInfo = ((HeavyRainfallFloodInfoResult.DataBean) intent.getSerializableExtra("actionInfo"));
        subSum = ((HeavyRainfallFloodReportResult.DataBean.SubSumBean) intent.getSerializableExtra("subSum"));

        reportsInfo = ((HeavyRainfallFloodReportResult.DataBean.ReportsBean) intent.getSerializableExtra("reportsInfo"));
    }

    @Override
    public void initView() {
        if (actionInfo != null) {
            mEtActionC1.setText(actionInfo.getActionC1());
            mEtActionC2.setText(actionInfo.getActionC2());
            mEtActionC3.setText(actionInfo.getActionC3());
            mEtActionC4.setText(actionInfo.getActionC4());
            mEtActionC5.setText(actionInfo.getActionC5());
            mEtActionC6.setText(actionInfo.getActionC6());

            mEtStaPersonCount.setText(actionInfo.getStaPersonCount());
            mEtStaCarCount.setText(actionInfo.getStaCarCount());
            etStaBoatCount.setText(actionInfo.getStaBoatCount());
            mEtStaSafeCount.setText(actionInfo.getStaSafeCount());
            mEtStaEvacuateCount.setText(actionInfo.getStaEvacuateCount());
            mEtStaRescueProperty.setText(actionInfo.getStaRescueProperty());
            mEtStaSafeProperty.setText(actionInfo.getStaSafeProperty());
        } else {
            if (subSum != null) {
                mEtActionC1.setText(subSum.getActionC1());
                mEtActionC2.setText(subSum.getActionC2());
                mEtActionC3.setText(subSum.getActionC3());
                mEtActionC4.setText(subSum.getActionC4());
                mEtActionC5.setText(subSum.getActionC5());
                mEtActionC6.setText(subSum.getActionC6());

                mEtStaPersonCount.setText(subSum.getStaPersonCount());
                mEtStaCarCount.setText(subSum.getStaCarCount());
                etStaBoatCount.setText(subSum.getStaBoatCount());
                mEtStaSafeCount.setText(subSum.getStaSafeCount());
                mEtStaEvacuateCount.setText(subSum.getStaEvacuateCount());
                mEtStaRescueProperty.setText(subSum.getStaRescueProperty());
                mEtStaSafeProperty.setText(subSum.getStaSafeProperty());
            }
        }

        /**
         * 从已上报列表跳转过来的，不可编辑，隐藏保存按钮
         */
        if (reportsInfo != null) {
            mEtActionC1.setText(reportsInfo.getActionC1());
            mEtActionC2.setText(reportsInfo.getActionC2());
            mEtActionC3.setText(reportsInfo.getActionC3());
            mEtActionC4.setText(reportsInfo.getActionC4());
            mEtActionC5.setText(reportsInfo.getActionC5());
            mEtActionC6.setText(reportsInfo.getActionC6());

            mEtStaPersonCount.setText(reportsInfo.getStaPersonCount());
            mEtStaCarCount.setText(reportsInfo.getStaCarCount());
            etStaBoatCount.setText(reportsInfo.getStaBoatCount());
            mEtStaSafeCount.setText(reportsInfo.getStaSafeCount());
            mEtStaEvacuateCount.setText(reportsInfo.getStaEvacuateCount());
            mEtStaRescueProperty.setText(reportsInfo.getStaRescueProperty());
            mEtStaSafeProperty.setText(reportsInfo.getStaSafeProperty());

            mEtActionC1.setEnabled(false);
            mEtActionC2.setEnabled(false);
            mEtActionC3.setEnabled(false);
            mEtActionC4.setEnabled(false);
            mEtActionC5.setEnabled(false);
            mEtActionC6.setEnabled(false);

            mEtStaPersonCount.setEnabled(false);
            mEtStaCarCount.setEnabled(false);
            etStaBoatCount.setEnabled(false);
            mEtStaSafeCount.setEnabled(false);
            mEtStaEvacuateCount.setEnabled(false);
            mEtStaRescueProperty.setEnabled(false);
            mEtStaSafeProperty.setEnabled(false);

            mTvSave.setVisibility(View.GONE);
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_save})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                if (actionInfo == null) {
                    actionInfo = new HeavyRainfallFloodInfoResult.DataBean();
                    setData();
                    saveReportInfo(actionInfo);
                } else {
                    setData();
                    updateReportInfo(actionInfo);
                }
                break;
        }
    }

    private void setData() {
        actionInfo.setActionC1(TextUtils.isEmpty(mEtActionC1.getText().toString()) ? "0" : mEtActionC1.getText().toString());
        actionInfo.setActionC2(TextUtils.isEmpty(mEtActionC2.getText().toString()) ? "0" : mEtActionC2.getText().toString());
        actionInfo.setActionC3(TextUtils.isEmpty(mEtActionC3.getText().toString()) ? "0" : mEtActionC3.getText().toString());
        actionInfo.setActionC4(TextUtils.isEmpty(mEtActionC4.getText().toString()) ? "0" : mEtActionC4.getText().toString());
        actionInfo.setActionC5(TextUtils.isEmpty(mEtActionC5.getText().toString()) ? "0" : mEtActionC5.getText().toString());
        actionInfo.setActionC6(TextUtils.isEmpty(mEtActionC6.getText().toString()) ? "0" : mEtActionC6.getText().toString());

        actionInfo.setStaPersonCount(TextUtils.isEmpty(mEtStaPersonCount.getText().toString()) ? "0" : mEtStaPersonCount.getText().toString());
        actionInfo.setStaCarCount(TextUtils.isEmpty(mEtStaCarCount.getText().toString()) ? "0" : mEtStaCarCount.getText().toString());
        actionInfo.setStaBoatCount(TextUtils.isEmpty(etStaBoatCount.getText().toString()) ? "0" : etStaBoatCount.getText().toString());
        actionInfo.setStaSafeCount(TextUtils.isEmpty(mEtStaSafeCount.getText().toString()) ? "0" : mEtStaSafeCount.getText().toString());
        actionInfo.setStaEvacuateCount(TextUtils.isEmpty(mEtStaEvacuateCount.getText().toString()) ? "0" : mEtStaEvacuateCount.getText().toString());
        actionInfo.setStaRescueProperty(TextUtils.isEmpty(mEtStaRescueProperty.getText().toString()) ? "0" : mEtStaRescueProperty.getText().toString());
        actionInfo.setStaSafeProperty(TextUtils.isEmpty(mEtStaSafeProperty.getText().toString()) ? "0" : mEtStaSafeProperty.getText().toString());
    }

    private void updateReportInfo(HeavyRainfallFloodInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().updateHeavyRainfallFloodReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }

    private void saveReportInfo(HeavyRainfallFloodInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().saveHeavyRainfallFloodReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }
}
